﻿using Anke.SHManage.BLL;
using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.IM.Controllers
{
    public class I_BalanceController : Controller
    {
        //
        // GET: /IM/I_Balance/

        public ActionResult BalanceList()
        {
            return View();
        }
        [HttpPost]
        public ActionResult DataLoad()
        {
            string MonthTime = Request.Form["MonthTime"].ToString();
            string BalanceType = Request.Form["BalanceType"].ToString();
            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);
            int rowCounts = 0;
            List<string> mTypeList = UserOperateContext.Current.getMaterialTypeLists(BalanceType);
            var list = new I_BalanceBLLExt().GetBalanceList(pageIndex, pageSize, MonthTime, mTypeList, ref rowCounts);
            return Json(new { total = rowCounts, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult GetMaxReportTime()
        {
            string MTypeID = Request.Form["MTypeID"].ToString();
            string reportMonth = "";
            if (new I_BalanceBLLExt().GetBalanceMax(MTypeID, ref reportMonth))
            {
                return Json(new { success = true, reportMonth = reportMonth }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { success = false, reportMonth = reportMonth }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
        }
        [HttpPost]
        public ActionResult SelectBalanceReport()
        {
            string mType = Request.Form["MType"].ToString();
            string reportMonth = Request.Form["ReportMonth"].ToString();
            List<string> mTypeList = UserOperateContext.Current.getMaterialTypeLists(mType);
            string TypeList = string.Join("','", mTypeList.ToArray());
            string errorMsg = "";
            var list = new I_BalanceBLLExt().GetI_BalanceNewList(reportMonth, TypeList, ref errorMsg);
            if (list != null)
            {
                if (list.Count > 0)
                    return Json(new { success = true, list = list, msg = "加载成功!" }, "appliction/json", JsonRequestBehavior.AllowGet);
                else
                    return Json(new { success = false, list = list, msg = "列表为空！请检查查询条件！" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { success = false, list = list, msg = errorMsg }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
        }
        [HttpPost]
        public ActionResult SubmitReport()
        {
            string ReportStr = Request.Form["ReportStr"].ToString();            
            List<I_Balance> list = JsonHelper.GetJsonInfoBy<List<I_Balance>>(ReportStr);
            string MType = Request.Form["MType"].ToString();
            string reportMonth = Request.Form["ReportMonth"].ToString();        
            string errorMsg = "";
            if (new I_BalanceBLLExt().AddBalance(list, MType, reportMonth, ref errorMsg))
                return this.JsonResult(Utility.E_JsonResult.OK, "报表生成成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, errorMsg, null, null);
        }

        /// <summary>
        /// 删除
        /// add2021-10-15 plq 医保上线移植
        /// </summary>
        /// <param name="ReportTime"></param>
        /// <param name="MaterialTypeID"></param>
        /// <returns></returns>
        public ActionResult DeleteBalance(String ReportTime, String MaterialTypeID)
        {
            I_BalanceBLLExt bll = new I_BalanceBLLExt();
            List<string> mTypeList = UserOperateContext.Current.getMaterialTypeLists(MaterialTypeID);
            string TypeList = string.Join("','", mTypeList.ToArray());
            if (bll.DeleteBalance(ReportTime, TypeList, MaterialTypeID))
            {
                return Json(new { Result = "OK", Message = "删除成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Result = "Error", Message = "删除失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            }
        }

    }
}
